using System.Collections.Generic;
using UnityEngine;

namespace Core
{
    public enum Language
    {
        en,
        cn,
    }
    public class LangComponent : Entity, IAwake
    {
        private Language language = Language.en;
        private Dictionary<string, string> dict = new Dictionary<string, string>();

        void IAwake.Awake()
        {
            ChangeLanguage(Language.cn);
        }

        public void ChangeLanguage(Language language)
        {
            if (language == this.language)
                return;

            dict.Clear();
            this.language = language;
            var asset = World.GetComponent<ResourceComponent>().Load<TextAsset>($"Lang/{language}");
            if (asset == null)
                return;

            var lines = asset.text.Split('\n');
            foreach (var line in lines)
            {
                var array = line.Trim().Split('=');
                dict[array[0]] = array[1];
            }
        }

        public string Get(string key)
        {
            dict.TryGetValue(key.ToLower(), out var value);
            return value ?? key;
        }
    }
}